package com.shuorikeji.huohuo.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsInitCallback;
import com.kwad.sdk.api.SdkConfig;
import com.qq.e.ads.cfg.MultiProcessFlag;
import com.qq.e.comm.managers.GDTAdSdk;
import com.qq.e.comm.managers.setting.GlobalSetting;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.home.HomeActivity;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.CountDownTimer;
import com.shuorikeji.huohuo.utils.DialogUtil;
import com.shuorikeji.huohuo.utils.InitUtils;
import com.shuorikeji.huohuo.utils.PreUtils;
import com.shuorikeji.huohuo.utils.UserDataObtainController;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class SplashActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.tv_djs)
    TextView tvDjs;

    private CountDownTimer djsTimer;
    private boolean isAgreeYs;
    private Dialog ysDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_splash);
    }

    @Override
    protected void initData() {
        isAgreeYs = (boolean) PreUtils.get(Constant.IS_AGREE_PRIVATE_RULE, false);
        if (isAgreeYs) {
            startDjs();
        } else {
            showYsRuleDialog();
        }
    }

    private void showYsRuleDialog() {
        ysDialog = DialogUtil.showYszc(SplashActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        finish();
                        break;
                    case 2:
                        PreUtils.put(Constant.IS_AGREE_PRIVATE_RULE, true);
                        initTxAdSdk();
                        initKS();
                        initCsjAd();
                        startDjs();
                        break;
                }
            }
        });
    }

    private void startDjs() {
        djsTimer = new CountDownTimer(3, 1000);
        djsTimer.start(new CountDownTimer.OnTimerCallBack() {
            @Override
            public void onStart() {
                Log.i("wxl", "验证码倒计时开始");
            }

            @Override
            public void onTick(long times) {
                tvDjs.setText(times + "s");
            }

            @Override
            public void onFinish() {
                Log.i("wxl", "验证码倒计时结束");
                Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initTxAdSdk() {
        Map<String, Boolean> params = new HashMap<>();
        params.put("cell_id", true);//false为关闭粗略地理位置获取，不设置或者设置为true为获取
        GlobalSetting.setAgreeReadPrivacyInfo(params);
        GlobalSetting.setChannel(3);
        /*支持多进程，仅限于和 DownloadService 的不在同一进程的 Activity*/
        MultiProcessFlag.setMultiProcess(false);
        //4.560.1430及以后版本
        GDTAdSdk.initWithoutStart(mContext, Constant.TX_YLH_APP_ID); // 该接口不会采集用户信息
        // 调用initWithoutStart后请尽快调用start，否则可能影响广告填充，造成收入下降
        GDTAdSdk.start(new GDTAdSdk.OnStartListener() {
            @Override
            public void onStartSuccess() {
                // 推荐开发者在onStartSuccess回调后开始拉广告
                Log.d("wxl", "腾讯优量汇开启");
            }

            @Override
            public void onStartFailed(Exception e) {
                Log.e("wxl", "gdt onStartFailed:" + e.toString());
            }
        });
    }

    private void initKS() {
        KsAdSDK.init(mContext, new SdkConfig.Builder().appId(Constant.KS_APP_ID) // 测试aapId，请联系快手平台申请正式AppId，必填
//                .appName(appName) // 测试appName，请填写您应用的名称，非必填
                .showNotification(false) // 是否展示下载通知栏，非必填
                .customController(UserDataObtainController.getInstance()) // 控制SDK获取用户设备信息接口，非必填
                .debug(true).setInitCallback(new KsInitCallback() {
                    @Override
                    public void onSuccess() {
                        KsAdSDK.start();
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        Log.i("wxl", "快手init fail code:" + code + "--msg:" + msg);
                    }
                }).setStartCallback(new KsInitCallback() {
                    @Override
                    public void onSuccess() {
                        Log.i("wxl", "快手start success");
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        Log.i("wxl", "快手start fail msg: " + msg);
                    }
                }).build());
    }

    private void initCsjAd() {
        InitUtils.csj(this, new InitUtils.InitCallback() {
            @Override
            public void success() {
                InitUtils.smallVideo(getBaseContext(), new InitUtils.InitCallback() {
                    @Override
                    public void success() {
                        Log.i("wxl", "穿山甲广告success: " + TTAdSdk.isSdkReady());
                        Intent intent = new Intent(SplashActivity.this, HomeActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void fail(int code, String msg) {

                    }
                });
            }

            @Override
            public void fail(int code, String msg) {

            }
        });
    }

    /**
     * 清理计时器
     */
    private void clearYzmDjs() {
        if (djsTimer != null) {
            djsTimer.cancel();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            clearYzmDjs();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onPause() {
        super.onPause();
        clearYzmDjs();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearYzmDjs();
        BusinessUtils.clearDialog(ysDialog);
    }
}